<?php

/**
 * 微信投票权手 Service
 * 
 * @path Apps/Common/Service/WxEventVotePlayerService.class.php
 * @author Zix <zix2002@gmail.com> 
 * @version v1.0 , 2015-09-11
 */

namespace Common\Service;

class WxEventVotePlayerService {

  private static $instance;

  var $status = array(
    0 => '待审核' ,
    1 => '已审核' ,
  );
  
  var $sex = array(
    1 => '男' ,
    2 => '女' , 
    3 => '保密' ,
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventVotePlayerService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'truename' => '',
      'nickname' => '',
      'sex' => 1 ,
      'age' => 1 ,
      'status' => 0 ,
      'user_id' => '',
      'icon' => '',
      'vote_count' => '',
      'desc' => '',
      'desc1' => '',
      'desc2' => '',
      'create_time' => date('Y-m-d H:i:s'),
    );
  }

  function get_by_id($id) {
    $WxEventVotePlayer = M('WxEventVotePlayer');
    $data = $WxEventVotePlayer->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'keyword' => '',
      'event_id' => '',
      'page' => 1,
      'page_size' => 6,
      'status' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxEventVotePlayer = M('WxEventVotePlayer');

    $where = array();
    
    if ( !empty( $config['keyword'] ) ) {
      $k = $config['keyword'];
      $where['_string'] = " evp.nickname = '$k' or evp.code = '$k' ";
    }
    
    if (!empty($config['status'])) {
      $where['evp.status'] = $config['status'];
    }

    if (!empty($config['event_id'])) {
      $where['evp.event_id'] = $config['event_id'];
    }

    if ($config['count']) {
      return $WxEventVotePlayer->alias('evp')->where($where)->count();
    } else {
      $order = 'evp.' . $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventVotePlayer
          ->alias('evp')
          ->field('evp.* , u.truename as u_truename , u.nickname as u_nickname')
          ->join('mer_user u on u.id = evp.user_id', 'left')
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $WxEventVotePlayer->_sql();
      return $data ? $data : array();
    }
  }

  function find( $where ) {
    $WxEventVotePlayer = M('WxEventVotePlayer');
    $data = $WxEventVotePlayer->where($where)->find();
    
    return $data ? $data : array();
  }
    
  
  
  function create($data) {
    $old_where = array(
      'event_id' => $data['event_id'] ,
      'user_id' => $data['user_id'] ,
    );
    
    $old_data = $this->find($old_where);
    if ( !empty( $old_data ) ) {
      return ajax_arr('您已经报过名了');
    }
    
    $WxEventVotePlayer = M('WxEventVotePlayer');
    $data['code'] = $this->_get_code($data['event_id']);
    $ret = $WxEventVotePlayer->add($data);
    if ($ret === FALSE) {
      return ajax_arr('添加失败', FALSE);
    } else {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    }
  }
  
  function _get_code( $event_id ) {
    $WxEventVotePlayer = M('WxEventVotePlayer');
    $count = $WxEventVotePlayer->where('event_id = %d' , $event_id)->count('id');
    return str_pad( $count+1 , 4, '0', STR_PAD_LEFT);
    
  }
    

  function update($id, $data) {
    $WxEventVotePlayer = M('WxEventVotePlayer');

    $ret = $WxEventVotePlayer->where("id = %d", $id)->save($data);
    if ($ret === FALSE) {
      return ajax_arr('编辑失败', FALSE);
    } else {
      return ajax_arr('编辑成功', TRUE);
    }
  }

  function delete($ids) {
    $WxEventVotePlayer = M('WxEventVotePlayer');
    $ret = $WxEventVotePlayer->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  function add_vote( $id ) {
    $WxEventVotePlayer = M('WxEventVotePlayer');
    $WxEventVotePlayer->where('id = %d' , $id )->setInc('vote_count');
  }
}
